import wxApi from "./utils/wxApi.js";
const mtjwxsdk = require("./utils/mtj-wx-sdk.js");
var api = require('./config/api.js');
let requests = require('./config/request');
App({
  
  globalData: {
    userInfo: null,
    getUserDone: 0,
    apiToken: '',
    knight: null,
    knightApply: null,
    hdimg: [],
    courier: null,
    first: false,
    smsTime: 0,
    shareInfo: {
      title: '校运通',
      desc: '校运通',
      imageUrl: '/images/logo.png',
      path: '/pages/shop/shop',
      success: function (res) {
        console.log('分享成功')
        console.log(res)
      },
    },
    schoolInfo: {},
    shareId: ''
  },

  onLaunch: function() {
    //获取用户的登录信息
    this.wxLogin();
  },
  
  ...wxApi,

  getUserInfo: function () {
    if (this.globalData.userInfo) {
      return true;
    }

    wx.navigateTo({
      url: '/pages/login/login'
    })
  },

  // 微信登录，获取openid
  wxLogin: function () {
    this.globalData.first = false;
    wx.showLoading({
      title: '获取信息中',
      mask: true,
    })
    var that = this;
    wx.login({
      success: function (res) {
        console.log(res);
        //return;
        if (res.code) {
          that.userLogin(res.code);
        } else {
          wx.hideLoading();
          console.log('登录失败！' + res.errMsg)
          // wx.showToast({
          //   title: '登录失败！请重试！',
          //   icon: 'none',
          //   duration: 2000
          // })
        }
      }
    });
  },

  //用户code登录
  userLogin: function (code) {
    var that = this;
    wx.request({
      url: api.WxLogin,
      data: {
        code: code
      },
      method: "POST",
      success: function (res) {
        wx.hideLoading();
        console.log(res);
        if (!res.data){
          wx.showToast({
            title: '系统错误！',
            icon: 'none',
            duration: 10000
          })
          return;
        }
        requests.getApiToken(res.data.data.token).then((token) => {
          that.globalData.apiToken = token;
          wx.setStorageSync('apiToken', token)
        })

        
        that.globalData.userInfo = res.data.data;
        that.globalData.getUserDone = 1;
        that.globalData.knight = res.data.knight;
        that.globalData.knightApply = res.data.knightApply;
        that.globalData.hdimg = res.data.bannerList;
        that.globalData.courier = res.data.courier;
        that.globalData.schoolInfo = res.data.schoolInfo;

        that.userInfoCallBack(res.data.data);

        var shareInfo = that.globalData.shareInfo;
        shareInfo.path = '/pages/index/index?shareId=' + that.globalData.userInfo.uCode;
        that.globalData.shareInfo = shareInfo;

        wx.navigateBack({
          delta: 1
        })
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.data);
        wx.showToast({
          title: '登录失败！请重试！',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  // 用户信息回调函数
  userInfoCallBack: function (userInfo) {},

  getApiToken(callback) {
    if(typeof callback !== 'function') {
      return this.globalData.apiToken;
    }
    wx.showLoading({
      title: '加载中...',
    })
    let loadingTime = 0;
    let index = setInterval(() => {
      if(loadingTime >= 30000) {
          wx.showToast({
            title: '授权失败',
            icon: 'none',
            duration: 2000
          })
          clearInterval(index)
          return;
      }
      if(this.globalData.apiToken){
        wx.hideLoading()
        clearInterval(index);
        callback(this.globalData.apiToken)
        return;
      }
      loadingTime += 50;
    }, 50)
  },

  checkReturn: function(data){
    if (data.code != 1){
      wx.showToast({
        title: '请求失败：' + data.msg,
        icon: 'none',
        duration: 2000
      })
      return false;
    }

    return true
  }
})